import os
import sys
import datetime
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

    def test_login(self):
        self.browser.get('http://likeshop.abc/admin/account/login.html')
        sleep(1)
        # 定位用户名输入并输入用户名
        self.browser.find_element(By.NAME,'account').send_keys('admin')
        # 定位密码输入框并输入密码
        self.browser.find_element(By.NAME,'password').send_keys('admin')
        # 定位登录按钮并点击
        self.browser.find_element(By.ID,'login').click()
        sleep(2)
    def test_query_shop(self):    
        # 点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        # 定位iframe子框架
        iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        # 定位商品名称搜索框
        self.browser.find_element(By.ID,'keyword').send_keys('文具')
        # 定位查询按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
        # 初始化变量
        is_not_bug = True
        # 商品列表
        shop_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(shop_list) == 0:
            print('未查询到商品')
        else:
            for shop in shop_list:
                # 商品名称
                shop_name = shop.find_element(By.XPATH,'./td[2]/div').text
                if '文具' not in shop_name:
                    print('有BUG')
                    is_not_bug = False
                    break
        if is_not_bug:
                    print('没有BUG')
    def test_out(self):
        # 退出子框架
        self.browser.switch_to.default_content()
        sleep(2)
        # 定位用户信息
        df = self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/a/cite')
        # 鼠标悬停
        ActionChains(self.browser).move_to_element(df).perform()
        sleep(2)
        # 点击退出
        self.browser.find_element(By.XPATH,'//*[@id="LAY_app"]/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
        sleep(2)
    def test_add_shop(self):
        """
        发布商品测试
        """
        # 点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        # 定位iframe子框架
        iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        # 定位商品发布按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        # 定位商品发布子框架
        iframe_add = self.browser.find_element(By.XPATH,'//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        # 定位商品名称输入框并输入商品名称
        now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
        self.browser.find_element(By.NAME,'name').send_keys('测试文具'+now)
        sleep(2)
        # 定位第一个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        # 定位第一个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/dl/dd[2]').click()
        sleep(2)
        # 定位第二个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        # 定位第二个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/dl/dd[2]').click()
        sleep(2)
        # 定位第三个分类的选项框并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        # 定位第三个分类的下拉项并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/dl/dd[2]').click()
        sleep(2)
        
        # 定位商品主图的添加图片按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(1)
        self.test_upload_img('D:\\SoftwareTest\\project\\cover1.jpg', '//*[@id="layui-layer-iframe2"]') 
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 切换到发布商品子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        # 定位商品轮播图的添加图片按钮并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(1)
        self.test_upload_img('D:\\SoftwareTest\\project\\cover1.jpg', '//*[@id="layui-layer-iframe3"]') 
         # 返回商品子框架
        self.browser.switch_to.parent_frame()
        # 切换到发布商品子框架
        self.browser.switch_to.frame(iframe_add)
        sleep(2)
        # 切换到价格设置标签页
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)
        # 定位市场价格输入框并输入市场价
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[2]/input').send_keys('100')
        sleep(2)
        # 定位价格输入框并输入价格
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[3]/input').send_keys('90')
        sleep(2)
        # 定位成本价格输入框并输入成本价格
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[4]/input').send_keys('80')
        sleep(2)
        # 定位库存输入框并输入库存数量
        self.browser.find_element(By.XPATH,'//*[@id="one-spec-lists-table"]/tbody/tr/td[5]/input').send_keys('100')
        sleep(2)
        # 切换到销售设置标签页
        self.browser.find_element(By.XPATH,'/html/body/div/ul/li[4]').click()
        sleep(2)
        # 定位配送方式的快递发货并点击
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[4]/div[6]/div/div[1]/span').click()
        sleep(1)
        # 返回商品子框架
        self.browser.switch_to.parent_frame()
        sleep(1)
        # 点击保存按钮
        self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
        sleep(3)
        
    def test_sj_shop(self):
        """
        测试上架商品
        """
         # 确保回到主框架
        self.browser.switch_to.default_content()
        sleep(2)
        # 点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        # 定位iframe子框架
        iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
        # 定位仓库中商品切换标签页
        self.browser.find_element(By.XPATH,'/html/body/div/div/div[2]/ul/li[2]').click()
        sleep(2)
        # # 接收商品列表
        goods_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        sleep(2)
        if len(goods_list) > 0:
            # 定位第一个商品的上架按钮并点击
            goods_list[0].find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr/td/div/a[2]').click()
            sleep(2)
            # 定位确认上架按钮并点击
            self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
            sleep(2)
        else:
            print('仓库中暂无商品')
    
    def test_edit_shop(self):
        """
        测试编辑商品
        """
        # 确保回到主框架
        self.browser.switch_to.default_content()
        sleep(2)
       # 点击商品按钮
        self.browser.find_element(By.XPATH,'//*[@id="LAY-system-side-menu"]/li[1]/a/cite').click()
        sleep(2)
        # 定位iframe子框架
        iframe = self.browser.find_element(By.XPATH,'//*[@id="LAY_app_body"]/div[2]/iframe')
        self.browser.switch_to.frame(iframe)
        sleep(2)
         # 商品列表
        shop_list = self.browser.find_elements(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if len(shop_list) > 0:
            # 定位第一个商品的编辑按钮并点击
            shop_list[0].find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr[1]/td/div/a[1]').click()
            sleep(2)
             # 定位商品发布子框架
            iframe_edit = self.browser.find_element(By.XPATH,'//*[@id="layui-layer-iframe1"]')
            self.browser.switch_to.frame(iframe_edit)
            sleep(2)
            # 清楚商品名称输入框内容
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').clear()
            sleep(2)
            now = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
            # 定位商品名称输入框并输入新名称
            self.browser.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[1]/div/input[2]').send_keys('新商品名称'+now)
            sleep(2)
            # 返回子框架
            self.browser.switch_to.parent_frame()
            # 点击保存按钮
            self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
            sleep(2)
        else:
            print('仓库中暂无商品')
    
    def test_upload_img(self, img_path, xpath_path):
        # 返回上商品子框架
        self.browser.switch_to.parent_frame()
        # 定位图片上传子框架并进入
        iframe_img = self.browser.find_element(By.XPATH,xpath_path)
        self.browser.switch_to.frame(iframe_img)
        sleep(2)
        # 定位图片上传输入框并上传图片
        self.browser.find_element(By.XPATH,'/html/body/div/div[2]/div[1]/div[1]/input').send_keys(img_path)
        sleep(4)
        # 选择第一张图片
        self.browser.find_element(By.XPATH,'/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
        sleep(2)
        # 点击使用图片按钮
        self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
        sleep(2)
        
        
        
        
       
if __name__ == '__main__':
    lk = LikeShopTest()
    lk.test_login()
    # lk.test_query_shop()
    lk.test_add_shop()
    lk.test_sj_shop()
    lk.test_edit_shop()
    lk.test_out()